package repo

import (
	"errors"
	"storeManageApi/models/dbModels"
	"storeManageApi/models/dbModels/reflectDbModels"
	"xorm.io/builder"
)

// IOutNotesRepo
//
//	@Description:出库表接口
type IOutNotesRepo interface {
	AddRow(row dbModels.OutNotes) (int64, error)
	GetPage(start, pageSize int) ([]dbModels.OutNotes, error)
	GetCount() (int64, error)
}

// CreateOutNotesRepo
//
//	@Description: 创建出库表repo
//	@return: IInNotesRepo
func CreateOutNotesRepo() IOutNotesRepo {
	var outNotesTable reflectDbModels.OutNotesTable = reflectDbModels.GetOutNotesTable()
	var storeNotesTable reflectDbModels.StoreNotesTable = reflectDbModels.GetStoreNotesTable()
	var goodsConfigsTable reflectDbModels.GoodsConfigsTable = reflectDbModels.GetGoodsConfigsTable()

	return &OutNotesRepo{
		OutNotesTable:     outNotesTable,
		StoreNotesTable:   storeNotesTable,
		GoodsConfigsTable: goodsConfigsTable,
	}
}

// OutNotesRepo
//
//	@Description:
type OutNotesRepo struct {
	StoreNotesTable   reflectDbModels.StoreNotesTable
	OutNotesTable     reflectDbModels.OutNotesTable
	GoodsConfigsTable reflectDbModels.GoodsConfigsTable
}

func (repo OutNotesRepo) GetCount() (int64, error) {
	return Engin.Table(repo.OutNotesTable.Table).Count()
}

func (repo OutNotesRepo) GetPage(start, pageSize int) ([]dbModels.OutNotes, error) {
	var results []dbModels.OutNotes = make([]dbModels.OutNotes, 0)
	err := Engin.Table(repo.OutNotesTable.Table).Desc(repo.OutNotesTable.Base.Id).Limit(pageSize, start-1).Find(&results)
	return results, err
}

// AddRow
//
//	@Description: 添加一条出库记录
//	@receiver: repo
//	@param: row
//	@return: int64
//	@return: error
func (repo OutNotesRepo) AddRow(row dbModels.OutNotes) (int64, error) {
	//  1.验证物品配置表中是否存在物品
	//  2.验证是否已有记录存在，有则更新，无则创建
	session := Engin.NewSession()
	defer session.Close()
	var goods dbModels.GoodsConfigs = dbModels.GoodsConfigs{}
	goodsBool, goodsErr := Engin.Table(repo.GoodsConfigsTable.Table).Where(builder.Eq{
		repo.GoodsConfigsTable.Base.Uid: row.GoodsUid,
	}).Get(&goods)
	if goodsErr != nil {
		return 0, goodsErr
	}
	if !goodsBool {
		return 0, errors.New("查询关联物品出错,请确认物品uid！")
	}
	var store dbModels.StoreNotes = dbModels.StoreNotes{}
	storeBool, storeErr := Engin.Table(repo.StoreNotesTable.Table).Where(builder.Eq{
		repo.StoreNotesTable.GoodsUid: row.GoodsUid,
	}).Get(&store)
	if storeErr != nil {
		return 0, storeErr
	}
	if storeBool {
		store.Count -= row.ActionNumber
		if store.Count < 0 {
			return 0, errors.New("库存余额不足，请先购置入库！")
		}
		if store.Count == 0 {
			store.Count = -1
		}
		row.EndCount = store.Count
		var status int = 2
		if store.Count <= store.SafeNumber {
			status = 1
		}
		if store.Count <= 0 {
			status = -1
		}
		store.Status = status
		if err := session.Begin(); err != nil {
			return 0, err
		}
		_, err := session.Table(repo.StoreNotesTable.Table).ID(store.Base.Id).Update(store)
		if err != nil {
			session.Rollback()
			return 0, err
		}
		_, rowErr := session.Insert(&row)
		if rowErr != nil {
			session.Rollback()
			return 0, rowErr
		}
		commitErr := session.Commit()
		if commitErr != nil {
			return 0, commitErr
		}
	} else {
		return 0, errors.New("库存记录不存在！请先添加库存记录！")
	}

	return 1, nil
}
